/**
 * Created by Administrator on 2015/10/9.
 */
var loginController = angular.module('login.controller', ['ngCordova','app.factory']);

loginController.controller('loginCtrl', ['$scope', '$state', '$ionicLoading', '$http', '$cordovaToast', 'DB','WEB_SERVICE', function ($scope, $state, $ionicLoading, $http, $cordovaToast, DB,WEB_SERVICE) {

  //登录控制器
  $scope.user = {
    userLab: '用户名',
    userName: '',
    passLab: '密码',
    passWord: '',
    submitLab: '登录'
  };
  $scope.onLoginClick = function () {

    //$cordovaToast.showShortCenter('数据同步失败。。。');
    $ionicLoading.show({
      template: 'Loading...'
    });
    //在线校验是否存在改用户
    var name = $scope.user.userName;
    var pass = $scope.user.passWord;

    ////本地储存

    $http({
      method: 'POST',
      url:'http://example8000.tunnel.mobi/MvnServiceExample/app/user/login',
      //url:WEB_SERVICE.url+WEB_SERVICE.method.select
      //url: 'http://example8000.tunnel.mobi/MvnServiceExample/ws/rest/service/select'
      params: {
        username: $scope.user.userName,
        password: $scope.user.passWord
      }
    })
      .success(function (data, status, headers, config) {

        $ionicLoading.hide();
        if(data.user == 'true'){
          $cordovaToast.showLongCenter('');

        }else{
          $cordovaToast.showLongCenter('用户不存在，请申请...');
        }
      })
      .error(function (data, status, headers, config) {

        $ionicLoading.hide();
        $cordovaToast.showLongCenter('连接失败，失败原因：' + data);

      });
  };
  $scope.hide = function () {
    $ionicLoading.hide();
  };

  //跳转到注册页面
  $scope.toRigister = function(){
    //$state.go('register');
    $cordovaToast.showShortBottom('查询数据...');
    DB.executeSql('select * from logs;',[],callback,1);
  };

  //数据查询回调函数
  var callback = function(flag,out){
    $cordovaToast.showLongTop('查询记录数：'+JSON.stringify(out));
  }

}])
;
